
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class Webpage2 extends StatefulWidget {
  const Webpage2({Key? key}) : super(key: key);

  @override
  _Webpage2State createState() => _Webpage2State();
}

class _Webpage2State extends State<Webpage2> {
  String title="加载中";
  late InAppWebViewController _controller;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:AppBar(
          centerTitle:true,
          title:Text("${title}"),
          leading:IconButton(
            icon:const Icon(Icons.arrow_back_ios),
            onPressed:() async{
              bool isgoback = await _controller.canGoBack();
              if(!isgoback){
                Navigator.pop(context);
              }else{
                await  _controller.goBack();
              }
            },
          )


      ),
        body:InAppWebView(
          contextMenu:ContextMenu(
            options:ContextMenuOptions(
              hideDefaultSystemContextMenuItems: false
            )
          ),
          initialOptions:InAppWebViewGroupOptions(
            crossPlatform:InAppWebViewOptions(
              cacheEnabled:false,
              mediaPlaybackRequiresUserGesture:false,
              disableContextMenu:false,
              javaScriptCanOpenWindowsAutomatically: true,
              javaScriptEnabled: true,
              userAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1 Edg/108.0.0.0"
            )
          ),
          onWebViewCreated:(InAppWebViewController controller) async{
              _controller = controller;
          },
          onLoadStart:(InAppWebViewController controller,url) async{

            setState(() {
              title="加载中";
            });

          },
          onLoadStop:(InAppWebViewController controller,url) async{
              print("加载完成");
          },
          onTitleChanged:(InAppWebViewController controller,title2) async{

              setState(() {
                 title=title2!;
              });
          },
          initialUrlRequest: URLRequest(url:Uri.parse("https://www.baidu.com/"),),
        ),
    );
  }
}
